﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Desaware.MachineLicense40;
using static System.Net.Mime.MediaTypeNames;
using System.Windows.Forms;

namespace WindowsFormsApp1
{

    internal class frmActivationCodeEntry : Form
    {
        private string _languageID = string.Empty;

        private string _fontName = "Arial";

        private float _fontSize = 8.25f;

        private byte _fontCharSet = 0;

        private ContextMenu _contextMenu1;

        private MenuItem _menuItemPaste;

        private string _activationCode = string.Empty;

        private Language _languageFile;

        private Font _normalFont;

        private Font _largeFont;

        private IContainer components = null;

        private CodeEntryControl codeEntryControl1;

        private Button cmdActivate;

        private Button cmdCopyTheLicense;

        internal Label lblLabel1;

        private Label lblLabel2;

        private Label lblLabel3;

        private Button cmdCancel;

        private Button cmdInstallLicense;

        private bool ActivationCodeIsGood
        {
            get
            {
                if (string.IsNullOrEmpty(codeEntryControl1.InstallCode))
                {
                    return false;
                }
                return true;
            }
        }

        public string ActivationCode => _activationCode;

        public event DoOptionClicked OptionClicked;

        public frmActivationCodeEntry(ClientLicense theClientLicense, bool displayCopyLicenseButton, string FontName, float FontSize, byte FontCharSet, Language LanguageFile)
        {
            _fontName = FontName;
            _fontSize = FontSize;
            _fontCharSet = FontCharSet;
            _languageFile = LanguageFile;
            _normalFont = new Font(_fontName, _fontSize, FontStyle.Regular, GraphicsUnit.Point, _fontCharSet);
            _largeFont = new Font(_fontName, _fontSize + 1f, FontStyle.Regular, GraphicsUnit.Point, _fontCharSet);
            base.FormClosed += frmActivationCodeEntry_FormClosed;
            Font = _normalFont;
            InitializeComponent();
            lblLabel1.Font = _largeFont;
            codeEntryControl1.License = theClientLicense;
            SetUpContextMenu();
            ApplyLocalization();
            codeEntryControl1.LicenseCodeEntered += codeEntryControl1_LicenseCodeEntered;
            codeEntryControl1.Controls[5].TextChanged += frmActivationCodeEntry_TextChanged;
            AdjustActivateButton();
            base.DialogResult = DialogResult.Cancel;
            cmdActivate.Click += cmdActivate_Click;
            cmdInstallLicense.Click += cmdInstallLicense_Click;
            if (displayCopyLicenseButton)
            {
                cmdCopyTheLicense.Visible = true;
                cmdCopyTheLicense.Click += cmdCopyTheLicense_Click;
            }
            else
            {
                cmdCopyTheLicense.Visible = false;
            }
        }

        private void frmActivationCodeEntry_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_normalFont != null)
            {
                _normalFont.Dispose();
                _normalFont = null;
            }
            if (_largeFont != null)
            {
                _largeFont.Dispose();
                _largeFont = null;
            }
        }

        private void cmdInstallLicense_Click(object sender, EventArgs e)
        {
            _activationCode = codeEntryControl1.InstallCode;
            ActivationCodeEntryEventArgs activationCodeEntryEventArgs = new ActivationCodeEntryEventArgs(activationOptions.InstallLicense, _activationCode, Cancel: false);
            SendOptionClicked(this, activationCodeEntryEventArgs);
            if (activationCodeEntryEventArgs.Cancel)
            {
                base.DialogResult = DialogResult.Cancel;
                Close();
            }
        }

        private void cmdCopyTheLicense_Click(object sender, EventArgs e)
        {
            if (!ActivationCodeIsGood)
            {
                return;
            }
            string text = _languageFile.GetLangString(71) + " " + _languageFile.GetLangString(72) + Environment.NewLine;
            string text2 = text;
            text = text2 + _languageFile.GetLangString(12) + " " + _languageFile.GetLangString(13) + Environment.NewLine;
            text += _languageFile.GetLangString(73);
            DialogResult dialogResult = MessageBox.Show(text, _languageFile.GetLangString(74), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                _activationCode = codeEntryControl1.InstallCode;
                ActivationCodeEntryEventArgs activationCodeEntryEventArgs = new ActivationCodeEntryEventArgs(activationOptions.CopyTheLicense, _activationCode, Cancel: false);
                SendOptionClicked(this, activationCodeEntryEventArgs);
                if (activationCodeEntryEventArgs.Cancel)
                {
                    base.DialogResult = DialogResult.Cancel;
                    Close();
                }
            }
        }

        private void cmdActivate_Click(object sender, EventArgs e)
        {
            if (ActivationCodeIsGood)
            {
                _activationCode = codeEntryControl1.InstallCode;
                base.DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void ApplyLocalization()
        {
            Text = _languageFile.GetLangString(1);
            base.Icon = Images.FormatSolutions_32x32_32_bit;
            lblLabel1.Text = _languageFile.GetLangString(2);
            lblLabel2.Text = _languageFile.GetLangString(3);
            lblLabel3.Text = _languageFile.GetLangString(4);
            cmdCopyTheLicense.Text = _languageFile.GetLangString(70);
            cmdActivate.Text = _languageFile.GetLangString(6);
            cmdCancel.Text = _languageFile.GetLangString(7);
            cmdInstallLicense.Text = _languageFile.GetLangString(8);
            _menuItemPaste.Text = _languageFile.GetLangString(9);
            base.Width = codeEntryControl1.Left * 2 + codeEntryControl1.Width;
            cmdActivate.Left = cmdCancel.Left - codeEntryControl1.Left - cmdActivate.Width;
            if (cmdCopyTheLicense.Width > cmdInstallLicense.Width)
            {
                cmdInstallLicense.Width = cmdCopyTheLicense.Width;
            }
            else
            {
                cmdCopyTheLicense.Width = cmdInstallLicense.Width;
            }
        }

        private void frmActivationCodeEntry_TextChanged(object sender, EventArgs e)
        {
            AdjustActivateButton();
        }

        private void codeEntryControl1_LicenseCodeEntered(object Sender, CodeEntryEventArgs e)
        {
            AdjustActivateButton();
        }

        private void AdjustActivateButton()
        {
            if (ActivationCodeIsGood)
            {
                cmdActivate.Enabled = true;
                cmdCopyTheLicense.Enabled = true;
                cmdInstallLicense.Enabled = true;
            }
            else
            {
                cmdActivate.Enabled = false;
                cmdCopyTheLicense.Enabled = false;
                cmdInstallLicense.Enabled = true;
            }
        }

        private void SetUpContextMenu()
        {
            _contextMenu1 = new ContextMenu();
            _menuItemPaste = new MenuItem();
            _contextMenu1.MenuItems.AddRange(new MenuItem[1] { _menuItemPaste });
            _menuItemPaste.Index = 0;
            _menuItemPaste.Click += menuItemPaste_Click;
            codeEntryControl1.Controls[5].ContextMenu = _contextMenu1;
            codeEntryControl1.Controls[4].ContextMenu = _contextMenu1;
            codeEntryControl1.Controls[3].ContextMenu = _contextMenu1;
            codeEntryControl1.Controls[2].ContextMenu = _contextMenu1;
            codeEntryControl1.Controls[1].ContextMenu = _contextMenu1;
            codeEntryControl1.Controls[0].ContextMenu = _contextMenu1;
        }

        private void menuItemPaste_Click(object sender, EventArgs e)
        {
            PasteClipboardCode();
        }

        private void PasteClipboardCode()
        {
            try
            {
                codeEntryControl1.InstallCode = Clipboard.GetDataObject().GetData(DataFormats.Text).ToString();
            }
            catch
            {
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            base.DialogResult = DialogResult.Cancel;
            Close();
        }

        protected virtual void SendOptionClicked(object sender, ActivationCodeEntryEventArgs e)
        {
            if (this.OptionClicked != null)
            {
                this.OptionClicked(sender, e);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.codeEntryControl1 = new Desaware.MachineLicense40.CodeEntryControl();
            this.cmdActivate = new System.Windows.Forms.Button();
            this.cmdCopyTheLicense = new System.Windows.Forms.Button();
            this.lblLabel1 = new System.Windows.Forms.Label();
            this.lblLabel2 = new System.Windows.Forms.Label();
            this.lblLabel3 = new System.Windows.Forms.Label();
            this.cmdCancel = new System.Windows.Forms.Button();
            this.cmdInstallLicense = new System.Windows.Forms.Button();
            base.SuspendLayout();
            this.codeEntryControl1.License = null;
            this.codeEntryControl1.Location = new System.Drawing.Point(15, 81);
            this.codeEntryControl1.Name = "codeEntryControl1";
            this.codeEntryControl1.Size = new System.Drawing.Size(353, 20);
            this.codeEntryControl1.TabIndex = 0;
            this.codeEntryControl1.ToolTip = null;
            this.cmdActivate.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.cmdActivate.AutoSize = true;
            this.cmdActivate.Enabled = false;
            this.cmdActivate.Location = new System.Drawing.Point(259, 210);
            this.cmdActivate.Name = "cmdActivate";
            this.cmdActivate.Size = new System.Drawing.Size(41, 27);
            this.cmdActivate.TabIndex = 1;
            this.cmdActivate.Text = "ACT";
            this.cmdActivate.UseVisualStyleBackColor = true;
            this.cmdCopyTheLicense.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
            this.cmdCopyTheLicense.AutoSize = true;
            this.cmdCopyTheLicense.Enabled = false;
            this.cmdCopyTheLicense.Location = new System.Drawing.Point(15, 178);
            this.cmdCopyTheLicense.Name = "cmdCopyTheLicense";
            this.cmdCopyTheLicense.Size = new System.Drawing.Size(119, 26);
            this.cmdCopyTheLicense.TabIndex = 3;
            this.cmdCopyTheLicense.Text = "COPY THE LICENSE";
            this.cmdCopyTheLicense.UseVisualStyleBackColor = true;
            this.lblLabel1.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
            this.lblLabel1.Location = new System.Drawing.Point(12, 7);
            this.lblLabel1.Name = "lblLabel1";
            this.lblLabel1.Size = new System.Drawing.Size(335, 34);
            this.lblLabel1.TabIndex = 22;
            this.lblLabel1.Text = "You need to activate Brill Formulation to continue.";
            this.lblLabel2.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
            this.lblLabel2.Location = new System.Drawing.Point(12, 114);
            this.lblLabel2.Name = "lblLabel2";
            this.lblLabel2.Size = new System.Drawing.Size(335, 45);
            this.lblLabel2.TabIndex = 29;
            this.lblLabel2.Text = "Activation ensures proper licensing and allows you to receive future software updates.";
            this.lblLabel3.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right;
            this.lblLabel3.Location = new System.Drawing.Point(12, 53);
            this.lblLabel3.Name = "lblLabel3";
            this.lblLabel3.Size = new System.Drawing.Size(335, 25);
            this.lblLabel3.TabIndex = 30;
            this.lblLabel3.Text = "Enter your activation code:";
            this.cmdCancel.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right;
            this.cmdCancel.AutoSize = true;
            this.cmdCancel.Location = new System.Drawing.Point(306, 210);
            this.cmdCancel.Name = "cmdCancel";
            this.cmdCancel.Size = new System.Drawing.Size(41, 27);
            this.cmdCancel.TabIndex = 33;
            this.cmdCancel.Text = "CAN";
            this.cmdCancel.UseVisualStyleBackColor = true;
            this.cmdCancel.Click += new System.EventHandler(cmdCancel_Click);
            this.cmdInstallLicense.Anchor = System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left;
            this.cmdInstallLicense.AutoSize = true;
            this.cmdInstallLicense.Enabled = false;
            this.cmdInstallLicense.Location = new System.Drawing.Point(15, 210);
            this.cmdInstallLicense.Name = "cmdInstallLicense";
            this.cmdInstallLicense.Size = new System.Drawing.Size(119, 27);
            this.cmdInstallLicense.TabIndex = 34;
            this.cmdInstallLicense.Text = "INSTALL A LICENSE";
            this.cmdInstallLicense.UseVisualStyleBackColor = true;
            base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 13f);
            base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            base.ClientSize = new System.Drawing.Size(359, 249);
            base.Controls.Add(this.cmdInstallLicense);
            base.Controls.Add(this.cmdCancel);
            base.Controls.Add(this.lblLabel3);
            base.Controls.Add(this.lblLabel2);
            base.Controls.Add(this.lblLabel1);
            base.Controls.Add(this.cmdCopyTheLicense);
            base.Controls.Add(this.cmdActivate);
            base.Controls.Add(this.codeEntryControl1);
            base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "frmActivationCodeEntry";
            base.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "ACTIVATION CODE ENTRY";
            base.ResumeLayout(false);
            base.PerformLayout();
        }
    }
}
